package pl.edu.agh.sius.ldap;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import pl.edu.agh.sius.schema.simplep2pserver.FileInfo;

public class User {

	private String username = StringUtils.EMPTY;
	private String address = StringUtils.EMPTY;
	private String files = " ";
	private String password = StringUtils.EMPTY;
	
	public User(){}
	
	public User(String username, String password) {
		this.username = username;
		this.password = password;
	}
	
	public User(String username, String password, String address) {
		this.username = username;
		this.password = password;
		this.address = address;
	}

	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getFiles() {
		return files;
	}
	public void setFiles(String files) {
		this.files = files;
	}
	public void setFiles(List<FileInfo> filesInfo) {
		String files = " ";
		for(FileInfo fileInfo : filesInfo) {
			files += fileInfo.getFileId()+","+fileInfo.getFileName()+","+fileInfo.getFileSize()+";";
		}
		this.files = files.substring(0, files.length()-1);
	}
	public List<FileInfo> getFilesList() {
		List<FileInfo> filesList = new ArrayList<FileInfo>();
		if(files!=null && !files.trim().isEmpty()) {
			for(String fileStr : files.split(";")) {
				if(!fileStr.trim().isEmpty()) {
					String[] fileData = fileStr.split(",");
					FileInfo fileInfo = new FileInfo();
					fileInfo.setFileId(new Long(fileData[0]));
					fileInfo.setFileName(fileData[1]);
					fileInfo.setFileSize(new Double(fileData[2]));
					filesList.add(fileInfo);
				}
			}
		}
		return filesList;
	}

	public void addFile(FileInfo fileInfo) {
		if(files==null) {
			files = " ";
		}
		if(!files.isEmpty()) {
			files += ";";
		}
		files += fileInfo.getFileId()+","+fileInfo.getFileName()+","+fileInfo.getFileSize();
	}

	public void removeFile(FileInfo selectedFile) {
		List<FileInfo> filesList = getFilesList();
		for(int i=0;i<filesList.size();i++) {
			if(filesList.get(i).getFileId()==selectedFile.getFileId()) {
				filesList.remove(i);
				break;
			}
		}
		setFiles(filesList);
	}
}
